package com.ldq.imooc.basics.io;

import java.io.*;

/**
 * @Author : 李道泉
 * @Description :
 * @Date : 2018/4/4 16:48
 * @Modified by :
 */
public class IOUtil {

    public static void printHex(String fileName) throws IOException {

        FileInputStream in = new FileInputStream(fileName);
        int b;
        int i = 1;
        while ((b = in.read()) != -1) {

            if (b <= 0xf) {
                System.out.print("0");
            }
            System.out.print(Integer.toHexString(b) + " ");

            if (i++ % 10 == 0) {
                System.out.println();
            }
        }
        in.close();
    }

    public static void printHexByByte(String fileName) throws IOException {
        FileInputStream in = new FileInputStream(fileName);
        byte[] buf = new byte[20 * 1024];

//        int bytes = in.read(buf, 0, buf.length);
//        int j = 1;
//        for (int i = 0; i < bytes; i++) {
//
//            //buf[i] & 0xff byte 8位 int 32位 避免数据转换错误，0xff把高24位清零
//            System.out.print(Integer.toHexString(buf[i] & 0xff) + " ");
//            if (j++ % 10 == 0) {
//                System.out.println();
//            }
//        }

        int bytes = 0;
        int j = 1;
        while ((bytes = in.read(buf, 0, buf.length)) != -1) {
            for (int i = 0; i < bytes; i++) {
                //buf[i] & 0xff byte 8位 int 32位 避免数据转换错误，0xff把高24位清零
                System.out.print(Integer.toHexString(buf[i] & 0xff) + " ");
                if (j++ % 10 == 0) {
                    System.out.println();
                }
            }
        }
    }

    /**
      * @Author : 李道泉
      * @Description : 字节批量读取拷贝文件
      * @Date 2018/4/17 13:51
      * @Modified by :
      */
    public static void copyFile(File srcFile, File destFile) throws IOException {

        if (!srcFile.exists()) {
            throw new IllegalArgumentException("文件：" + srcFile + "不存在！");
        }
        if (!srcFile.isFile()) {
            throw new IllegalArgumentException(srcFile + "不是文件！");
        }
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(destFile);
        byte[] buf = new byte[8 * 1024];
        int b;
        while ((b = in.read(buf, 0, buf.length)) != -1) {
            out.write(buf, 0, b);
            out.flush();
            out.close();
        }
    }

    /**
     * @Author : 李道泉
     * @Description : 文件拷贝，利用带缓冲的字节流
     * @Date 2018/4/17 13:34
     * @Modified by :
     */
    public static void copyFileByBuffer(File srcFile, File destFile) throws IOException {

        if (!srcFile.exists()) {
            throw new IllegalArgumentException("文件：" + srcFile + "不存在！");
        }
        if (!srcFile.isFile()) {
            throw new IllegalArgumentException(srcFile + "不是文件！");
        }
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
        int c;
        while ((c = bis.read()) != -1) {

            bos.write(c);
            bos.flush();
        }
        bis.close();
        bos.close();
    }

    /**
      * @Author : 李道泉
      * @Description : 单字节无缓冲拷贝文件
      * @Date 2018/4/17 13:49
      * @Modified by :
      */
    public static void copyFileByByte(File srcFile, File destFile) throws IOException {

        if (!srcFile.exists()) {
            throw new IllegalArgumentException("文件：" + srcFile + "不存在！");
        }
        if (!srcFile.isFile()) {
            throw new IllegalArgumentException(srcFile + "不是文件！");
        }
        FileInputStream in = new FileInputStream(srcFile);
        FileOutputStream out = new FileOutputStream(destFile);
        int c;
        while ((c = in.read()) != -1) {
            out.write(c);
            out.flush();
        }
        in.close();
        out.close();
    }
}
